from . import admin_blue


from flask import g, render_template, request, session, redirect, url_for, jsonify, current_app, abort

from info import constants, db
from info.models import User, News, Category
from info.utils.commons import login_required
from info.utils.image_storage import storage
from info.utils.response_code import RET
from . import admin_blue

@admin_blue.route('/index')
@login_required
def index():
    """后台管理首页"""
    user = g.user
    return render_template('admin/index.html',user=user.to_dict())


@admin_blue.route('/login',methods=['GET','POST'])
def login():
    """
    后台管理员登录
    1、如果为get请求，使用session获取登录信息，user_id,is_admin,
    2、判断用户如果用户id存在并是管理员，重定向到后台管理页面
    3、获取参数，user_name,password
    4、校验参数完整性
    5、查询数据库，确认用户存在，is_admin为true，校验密码
    6、缓存用户信息，user_id,mobile,nick_name,is_admin
    7、跳转到后台管理页面

    :return:
    """
    if request.method == 'GET':
        user_id = session.get('user_id',None)
        is_admin = session.get('is_admin',False)
        if user_id and is_admin:
            return redirect(url_for('admin.index'))
        return render_template('admin/login.html')

    user_name = request.form.get('username')
    password = request.form.get('password')
    if not all([user_name,password]):
        return render_template('admin/login.html', errmsg='参数不完整')
    try:
        user = User.query.filter(User.mobile==user_name,User.is_admin==True).first()
    except Exception as e:
        current_app.logger.error(e)
        return render_template('admin/login.html', errmsg='数据库查询错误')
    if user is None or not user.check_password(password):
        return render_template('admin/login.html', errmsg='用户名或密码错误')
    session['user_id'] = user.id
    session['mobile'] = user.mobile
    session['nick_name'] = user.nick_name
    session['is_admin'] = user.is_admin
    return redirect(url_for('admin.index'))


import time
from datetime import datetime,timedelta
@admin_blue.route('/user_count')
def user_count():
    total_count=0
    try:
        total_count=User.query.filter(User.is_admin==False).count()


    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询总人数异常')

    #月新增人数
    mon_count=0
    day_count=0

    #实例化时间对象，转换成日期的字符串
    t=time.localtime()
    mon_begin_date_str='%d-%02d-01'%(t.tm_year,t.tm_mon)


    #把日期格式化成日期对象
    mon_begin_date = datetime.strptime(mon_begin_date_str,'%Y-%m-%d')
    try:
        mon_count=User.query.filter(User.is_admin==False,User.create_time>mon_begin_date
                                    ).count()

    except Exception as e:
         current_app.logger.error(e)
         return jsonify(errno=RET.DBERR, errmsg='月人数异常')

    #日新增人数
    day_begin_date_str='%d-%02d-%02d'%(t.tm_year,t.tm_mon,t.tm_mday)
    day_begin_date = datetime.strptime(day_begin_date_str, '%Y-%m-%d')
    try:
        day_count = User.query.filter(User.is_admin == False, User.create_time > day_begin_date
                                      ).count()

    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询天新增人数异常')

    #定义容器，存储活跃的人数和日期
    active_count=[]
    active_time=[]
    active_begin_date_str='%d-%02d-%02d'%(t.tm_year,t.tm_mon,t.tm_mday)
    active_begin_date=datetime.strptime(active_begin_date_str, '%Y-%m-%d')

    for x in range(0,31):
        begin_date=active_begin_date-timedelta(days=(x))
        end_date=active_begin_date-timedelta(days=(x-1))
        try:
            count=User.query.filter(User.is_admin==False,User.last_login>=begin_date,
                                    User.last_login<end_date).count()
        except Exception as e:
            current_app.logger.error(e)
            return jsonify(errno=RET.DBERR, errmsg='查询日活跃人数异常')

        #日期对象不能放在模板中，把日期对象转换成字符串
        begin_date_str=datetime.strftime(begin_date,'%Y-%m-%d')
        active_count.append(count)
        active_time.append(begin_date_str)
    active_time.reverse()
    active_count.reverse()
    data={
        'total_count':total_count,
        'mon_count':mon_count,
        'day_count':day_count,
        'active_count': active_count,
        'active_time':active_time
    }
    return render_template('admin/user_count.html',data=data)

